package com.didispace.chapter310.service.obs;

import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.concurrent.ConcurrentHashMap;

public class ObservableIOC {
    private static Map<String, List<ObservalUser>> obsMap;

    public ObservableIOC(){
        obsMap=new ConcurrentHashMap<>();
    }
    public static  void addObsUser(String userKey,List<ObservalUser> observalUserList){
         obsMap.put(userKey,observalUserList);
    }
    public static void addSingleObsUser(String userKey,ObservalUser observalUser){
        obsMap.get(userKey).add(observalUser);
    }
    public static void deleteSingleObsUser(String userKey,ObservalUser observalUser){
        obsMap.get(userKey)
                .removeIf(observalUser1 -> observalUser.equals(observalUser1));

    }
    public static void deleteObsUser(String userKey){
        obsMap.remove(userKey);
    }
    public static void noticeSingleObsUser(String userKey,Object args){
        obsMap.get(userKey)
                .stream()
                .peek(observalUser -> observalUser.update(args))
                ;
    }
}
